using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;

namespace GiaoDien
{
    class SanPhamDAO
    {
        #region themThongTin
        public static bool themNguoiDung(SanPhamDTO spDTO)
        {
            string sQuery = "insert into ChitietSP(Masach,Tensach,Tacgia,NhaXB,Kichthuoc,Sotrang,Ngaysanxuat,Soluongcon,Dongia) values(" + spDTO.MaSach + ",'" + spDTO.TenSach + "','" +
                spDTO.TacGia + "','" + spDTO.NhaXB + "','" + spDTO.KichThuoc + "','" + spDTO.SoTrang + "','" + spDTO.NgaySanXuat + "','" + spDTO.SoLuongCon + "','" + spDTO.DonGia + "')";
            OleDbConnection con = DataProvider.taoKetNoi();
            bool ketqua = DataProvider.executeNonQuery(sQuery, con);
            DataProvider.dongKetNoi(con);
            return ketqua;
        }
        #endregion
		
		#region ThemSP
		public static bool ThemSP(SanPhamDTO spDTO)
        {
            string sQuery = "insert into SanPham(Masach,Tensach,Loaisach) values(" + spDTO.MaSach + ",'" + spDTO.TenSach + "','" +
                spDTO.LoaiSach + "')";
            OleDbConnection con = DataProvider.taoKetNoi();
            bool ketqua = DataProvider.executeNonQuery(sQuery, con);
            DataProvider.dongKetNoi(con);
            return ketqua;
        }
		#endregion

        #region layMaSach
        public static string LayMaSach(SanPhamDTO spDTO)
        {
            string sQuery = "select top 1 MaSach from ChitietSP order by MaSach DESC";
            OleDbConnection con = DataProvider.taoKetNoi();
            DataTable da = DataProvider.layDataTable(sQuery, con);
            DataProvider.dongKetNoi(con);
            DataRow dr = da.Rows[0];
            string MaSachMoi=dr["Masach"].ToString();
            return (int.Parse(MaSachMoi) + 1).ToString();
        }
        #endregion

        #region LayChiTietSanPham
        public static DataTable LayChiTietSanPham(SanPhamDTO spDTO)
        {
            string sQuery = "select * from ChitietSP order by MaSach";
            OleDbConnection con = DataProvider.taoKetNoi();
            DataTable da = DataProvider.layDataTable(sQuery, con);
            DataProvider.dongKetNoi(con);
            return da;
        }
        #endregion
		
        #region CapNhapThongTin
        public static bool CapNhapThongTin(SanPhamDTO spDTO)
        {
            string sQuery = "update ChitietSP set Tensach='" + spDTO.TenSach + "',Tacgia='" + spDTO.TacGia +
                 "',NhaXB='" + spDTO.NhaXB + "',Kichthuoc='" + spDTO.KichThuoc + "',Sotrang='" + spDTO.SoTrang
                + "',Ngaysanxuat='" + spDTO.NgaySanXuat + "',Soluongcon='" + spDTO.SoLuongCon + "',Dongia='" + spDTO.DonGia + "' where Masach=" + spDTO.MaSach;
            OleDbConnection con = DataProvider.taoKetNoi();
            bool ketqua = DataProvider.executeNonQuery(sQuery, con);
            DataProvider.dongKetNoi(con);
            return ketqua;
        }
        #endregion
		
		#region CapNhapSP
		public static bool CapNhapSP(SanPhamDTO spDTO)
        {
            string sQuery = "update SanPham set Tensach='" + spDTO.TenSach + "',Loaisach='" + spDTO.LoaiSach + "' where Masach=" + spDTO.MaSach;
            OleDbConnection con = DataProvider.taoKetNoi();
            bool ketqua = DataProvider.executeNonQuery(sQuery, con);
            DataProvider.dongKetNoi(con);
            return ketqua;
        }
		#endregion
		
		#region XoaSanPham
        public static bool XoaSanPham(SanPhamDTO spDTO)
        {
            string sQuery = "delete from ChiTietSP where Masach=" + spDTO.MaSach;
            OleDbConnection con = DataProvider.taoKetNoi();
            bool ketqua = DataProvider.executeNonQuery(sQuery, con);
            DataProvider.dongKetNoi(con);
            return ketqua;
        }
        #endregion
    }
}
